/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <vector>

class ListOfPorts
{
private:
	void CleanUp(void);

	bool AddPort(WORD);
	bool BuildPortRange(CString, int*, bool asPortIn);
	bool AddPortFromString(CString, bool, bool asPortIn);

	std::vector<WORD> theVector;
	std::vector<WORD>::iterator iter;

	int reqSize;

	WORD GetFirst(void);
	WORD GetNext(void);

public:
	ListOfPorts(void);
	~ListOfPorts(void);

	/*! \brief Loads a list of ports or protocols from a string.

		This function converts a user specified list of ports or protocols 
		in the required internal format.\n
		A valid string should be formed as follows:\n
			- the elements should be separated with commas
			- each element could be a value or a range of values
			- a range of values consists of two values separated by a dash
			- the '*' character means all acceptable values
			- white spaces are allowed
			.
		Example: "20, 22-25, 80, 110-139".\n

		Warning 1: No optimizations are made so if the list is something like "80, 80, 80" then
		the value "80" will be added three times in the list !\n

		Warning 2: The values are not checked for validity ! If a value exceeds the allowed limit then that value will be truncated.

		\param strIn the string from which to load the values
		\param asPortIn true if the string should be treated as a list of ports and false if we talk about protocols
	*/
	bool LoadFromString(CString strIn, bool asPortIn);

	void Clear(void);

	int GetSize(void);
	WORD GetByIdx(int);

	void SortTheList(void);
	void RandomizeList(void);

	bool AddOnePort(WORD);
};
